package meijuli.com.join51.business.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.activity.BaseFragmentActivity;
import meijuli.com.join51.business.adapter.ClientListAdapter;
import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import meijuli.com.join51.business.model.ClientInfo;
import meijuli.com.join51.business.model.ClientList;

import java.util.ArrayList;
import java.util.List;
import meijuli.com.join51.business.service.ApiService;
import retrofit2.Call;

/**
 * 我的粉丝
 *
 * @author wenqin
 */
public class MyClientListFragment extends BaseFragment {
    /**
     * 用户合计人数TextView
     */
    TextView clientNumberTextView;
    /**
     * 新增用户人数TextView
     */
    TextView newClientNumber;
    RecyclerView mRecyclerView;

    private ClientListAdapter mListAdapter;
    private List<ClientInfo> mClientInfoList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_client_list, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        clientNumberTextView = view.findViewById(R.id.client_number_text_view);
        newClientNumber = view.findViewById(R.id.newClientNumber);
        mRecyclerView = view.findViewById(R.id.recyclerview);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        BaseFragmentActivity activity = (BaseFragmentActivity) getActivity();
        activity.setToolBarTitle("我的粉丝");

        mClientInfoList = new ArrayList<>();
        mListAdapter = new ClientListAdapter(getActivity(), mClientInfoList);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mListAdapter);

        Call<HttpResponse<ClientList>> call = ApiManager.get()
                .getApiService(ApiService.class).getMyTeamFans();
        HttpRequest.getRequest().autoRecycle(this).start(call, new ResponseCallback<ClientList>() {

            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(String url, ClientList data) {
                dismissDialog();
                if (data != null) {
                    mClientInfoList.clear();
                    mClientInfoList.addAll(data.getFansList());
                    mListAdapter.notifyDataSetChanged();
                    clientNumberTextView.setText("用户合计: " + data.getTotalCount() + "人");
                    newClientNumber.setText(data.getNewNum() + "");
                }
            }
        });
    }

}
